home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17736 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.3 KB  |  190 lines

  1. Path: siemens.ch!news
  2. From: mrw@tobago.siemens.ch (Waeckerlin Marc)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Circular Usage (forward declaration?)
  5. Date: 17 Apr 1996 08:57:37 GMT
  6. Organization: Siemens Schweiz AG
  7. Message-ID: <MRW.96Apr17105737@tobago.siemens.ch>
  8. References: <MRW.96Apr16184800@tobago.siemens.ch>
  9.     <ltu3ykovzc.fsf@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  10.     <31747631.5970@datalytics.com> <MRW.96Apr17104121@tobago.siemens.ch>
  11. NNTP-Posting-Host: 150.10.54.28
  12. In-reply-to: mrw@tobago.siemens.ch's message of 17 Apr 1996 08:41:21 GMT
  13.  
  14. Hi there
  15.  
  16. You have the same problem in other cases (yes I know there is an endless loop):
  17.  
  18. class B;
  19.  
  20. class A
  21. {
  22.   public:
  23.     void b(B &x)
  24.     {
  25.       x.a(*this);
  26.     }
  27. };
  28.  
  29. class B
  30. {
  31.   public:
  32.     void a(A &x)
  33.     {
  34.       x.b(*this);
  35.     }
  36. };
  37.  
  38.  
  39. It does not even work with pointers:
  40.  
  41. class B;
  42.  
  43. class A
  44. {
  45.   public:
  46.     void b(B *x)
  47.     {
  48.       assert(x);
  49.       x->a(this);
  50.     }
  51. };
  52.  
  53. class B
  54. {
  55.   public:
  56.     void a(A x)
  57.     {
  58.       assert(x);
  59.       x->b(this);
  60.     }
  61. };
  62.  
  63.  
  64. -> is there a solution or not?
  65. BTW: regarding the smart p[ointer example below - I know, that circular
  66.      references and smart pointers normally don't work tokether, but you
  67.      don't know how smart *these* smart pointers may be ;-) ...
  68.  
  69.  
  70. regards
  71.  
  72.  
  73.  
  74. In article <MRW.96Apr17104121@tobago.siemens.ch> mrw@tobago.siemens.ch (Waeckerlin Marc) writes:
  75.  
  76. > From: mrw@tobago.siemens.ch (Waeckerlin Marc)
  77. > Newsgroups: comp.lang.c++
  78. > Date: 17 Apr 1996 08:41:21 GMT
  79. > Organization: Siemens Schweiz AG
  80. > Hi
  81. > Thank you for the answers.
  82. > I know this way, but it does not work in my case, since the members "x"
  83. > absolutely must be no pointers.
  84. > Speaking more exactly, they are pointers, but they are smart pointers.
  85. > In another example, you could get somnething like this, where "CPointer"
  86. > is a smart pointer:
  87. > class CCompany
  88. > {
  89. >   protected:
  90. >     CPointer<CEmployee> m_emp;
  91. >     
  92. >   public:
  93. >     set(CEmployee &p_emp)
  94. >     {
  95. >       m_emp = &p_emp;
  96. >     }
  97. > };
  98. > class CEmployee
  99. > {
  100. >   protected:
  101. >     CPointer<CCompany> m_comp;
  102. >   public:
  103. >     
  104. >     set(CCompany &p_comp)
  105. >     {
  106. >       m_comp = &p_comp;
  107. >     }
  108. > };
  109. > Best Regards
  110. > Marc
  111. > In article <31747631.5970@datalytics.com> Rob Stewart <stew@datalytics.com> writes:
  112. > > From: Rob Stewart <stew@datalytics.com>
  113. > > Newsgroups: comp.lang.c++
  114. > > Date: Wed, 17 Apr 1996 00:40:17 -0400
  115. > > Organization: Datalytics, Inc.
  116. > > 
  117. > > Enno Sandner wrote:
  118. > > > 
  119. > > > In article <MRW.96Apr16184800@tobago.siemens.ch> mrw@tobago.siemens.ch (Waeckerlin Marc) writes:
  120. > > > 
  121. > > >    How do you realize this in C++:
  122. > > >    (the whole problem is much more complex, but it can be reduced to that)
  123. > > > 
  124. > > >    class A
  125. > > >    {
  126. > > >        B x;
  127. > > >    };
  128. > > > 
  129. > > >    class B
  130. > > >    {
  131. > > >        A x;
  132. > > >    };
  133. > > > 
  134. > > > There's no way -- use pointers or references instead, i.e. dynamicly
  135. > > > allocate the appropriate instance of class 'A' or class 'B'.
  136. > > > 
  137. > > >         Enno
  138. > > 
  139. > > Enno is correct, except you also need to forward declare one of 
  140. > > the classes:
  141. > > 
  142. > > class B;
  143. > > 
  144. > > class A
  145. > > {
  146. > >    B* px;
  147. > > };
  148. > > 
  149. > > class B
  150. > > {
  151. > >    A x;
  152. > > };
  153. > > 
  154. > > -- 
  155. > > Rob Stewart    | My opinions are generally my own.  They do
  156. > > Datalytics, Inc.| not necessarily reflect those of my employer.
  157. > > 
  158. -- 
  159.                     \|||/             SIEMENS SCHWEIZ AG
  160.                     /Q Q\     
  161. ================oOOo=\Y/=oOOo====================================
  162. | Marc WΣckerlin             | E-mail: mrw@siemens.ch           |
  163. | Siemens Schweiz EUQ2/I-49  |                                  |
  164. | Albisriederstrasse 245     | Phone:  ++41 1 495 32 29         |
  165. | CH-8047 Zⁿrich             | Fax:    ++41 1 495 44 37         |
  166. |---------------------------------------------------------------|
  167. |                Homepage: http://www.siemens.ch                |
  168. \---------------------------------------------------------------/
  169. Visit my personal homepage at:  http://www.access.ch/~mwaeckerlin
  170.                     
  171.  _______________                                   ______________
  172. |joke of the day|                                 / just kidding \
  173. ==================================================================
  174. Smash forehead on keyboard to continue.....
  175. ==================================================================
  176.